package com.zzxz.practicaltrainingsys.service;

import com.zzxz.practicaltrainingsys.entity.ChatMessage;
import com.zzxz.practicaltrainingsys.repository.ChatMessageRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChatMessageService {

    @Autowired
    private ChatMessageRepository chatMessageRepository;

    public ChatMessage saveMessage(String sessionId, String senderType, String content) {
        ChatMessage message = new ChatMessage(sessionId, senderType, content);
        return chatMessageRepository.save(message);
    }

    public List<ChatMessage> getRecentMessages(String sessionId, int limit) {
        return chatMessageRepository.findBySessionIdOrderByCreateTimeDesc(sessionId, PageRequest.of(0, limit));
    }

    public List<ChatMessage> getAllMessages(String sessionId) {
        return chatMessageRepository.findBySessionIdOrderByCreateTimeAsc(sessionId);
    }
}